import {getGrabbingOrderApi,getFinishOrderApi} from '../../api/driver/order.js'

const state = {
	driverOrderList: [
		[],
		[],
		[]
	],
};

const mutations = {
	getGrabbingOrder: (state, {page,list}) => {
		if(page === 1){
			state.driverOrderList[0].splice(0)
		}
		state.driverOrderList[0].push(...list);
	},
	getFinishOrder: (state, {page,list}) => {
		if(page === 1){
			state.driverOrderList[1].splice(0)
		}
		state.driverOrderList[1].push(...list);
	},
	removeOrder: (state,orderCode) => {
		let orderIndex = state.driverOrderList[0].findIndex(item => item.orderCode === orderCode);
		state.driverOrderList[0][orderIndex].orderState = 4;
	},
	search0: (state,data) => {
		state.driverOrderList[0].push(...data);
	},
	searchfinish: (state,data) => {
		state.driverOrderList[1].push(...data);
	},
	clear: state => {
		state.driverOrderList = [[],[],[]]
	}
};

const actions = {
	GETGRABBINGORDER({
		commit
	}, parmas) {
		return new Promise((resolve, reject) => {
			// TODO 请求获取订单数据
			getGrabbingOrderApi(parmas).then(res => {
				if (res.header.code === 200) {
					commit('getGrabbingOrder', {page:res.body.currPage,list:res.body.list});
				}
				resolve(res.header);
			}).catch(err => {
				reject(err);
			});
		})
	},
	GETFINISHORDER({
		commit
	}, parmas) {
		return new Promise((resolve, reject) => {
			// TODO 请求获取订单数据
			getFinishOrderApi(parmas).then(res => {
				if (res.header.code === 200) {
					commit('getFinishOrder', {page:res.body.currPage,list:res.body.list});
				}
				resolve(res.header);
			}).catch(err => {
				reject(err);
			});
		})
	},
	SEARCHORDER({commit},{data,tab}) {
		console.log(data);
		if(tab === 0) {
			commit('search0',data.filter(item => item.orderState <= 4));
		}
		if(tab === 1) {
			commit('searchfinish',data.filter(item => item.orderState >= 5));
		}
	},
	
	REMOVEORDER({commit},data) {
		console.log(data);
		commit('removeOrder',data.orderCode);
	},
	
	CLEAR({commit}) {
		commit('clear');
	}
};

export default {
	namespaced: true,
	mutations,
	actions,
	state
}
